package vn.sunnet.game.SinhTon.screen;

import java.util.ArrayList;
import java.util.Iterator;

import vn.sunnet.game.SinhTon.global.Assets;
import vn.sunnet.game.SinhTon.global.Dimension;
import vn.sunnet.game.SinhTon.global.OverlapTester;
import vn.sunnet.game.SinhTon.global.Setting;
import vn.sunnet.game.SinhTon.map.InfoMap;
import vn.sunnet.game.SinhTon.map.Map;
import vn.sunnet.game.SinhTon.object.Blockhouse;
import vn.sunnet.game.SinhTon.object.Boss10;
import vn.sunnet.game.SinhTon.object.Boss11;
import vn.sunnet.game.SinhTon.object.Bullet;
import vn.sunnet.game.SinhTon.object.BulletBoss;
import vn.sunnet.game.SinhTon.object.ExplosiveDevice;
import vn.sunnet.game.SinhTon.object.FireObject;
import vn.sunnet.game.SinhTon.object.GunShopObject;
import vn.sunnet.game.SinhTon.object.MoveLetters;
import vn.sunnet.game.SinhTon.object.NipperLeft;
import vn.sunnet.game.SinhTon.object.NipperRight;
import vn.sunnet.game.SinhTon.object.PlusScore;
import vn.sunnet.game.SinhTon.object.WaveZombie;
import vn.sunnet.game.SinhTon.object.Zombie;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;

public class PlayScreen extends BaseScreen {
	// Dialog
	private Dialog dialogPause, dialogWin, dialogLose;
	// Button
	private Button btnPause, btnMenuPause, btnMenuLose, btnTiepTucGame,
			btnRetry1, btnRetry2, btnGuiDiemWin, btnGuiDiemLose;

	// boolean
	private boolean isPause, isFire;// Trang thai pause va ban
	private boolean isGun1 = true; // Dat bien kiem tra sung dang ban la true
	private boolean isDialogLose, isDialogWin;
	private boolean isRocket, isGrenade; // Trang thai su dung 2 loai skill
	private boolean isSound, isMusic; // luu am thanh trong game là On hay Off
	private boolean isVibrate; // co rung man hinh hay ko
	private boolean check;

	// int
	private final int MODE_BAN = 1;
	private final int MODE_SKILL = 2;
	private int modePlay = MODE_BAN; // Dat trang thai ban dau la dang ban
	// Trang thai cac chu dot 1 va dot cuoi chay ra
	private final int GAME_WAVE = 0;
	private final int GAME_RUNNING = 1;
	private final int GAME_WIN = 2;
	private final int GAME_LOSE = 3;
	// Trang thai cua game ban dau la GAME_RUNNING
	private int stateGame;

	private float x1, y1; // Tam quay cua dan
	private int random, numberZombie;
	private int numberZombieDie; // so Zombie da chet
	private int typeSkill; // loai skill dung trong game
	private int speed_Zombie = 1; // toc do cua zombie
	private int locot; // id cua locot dung trong gamePlay
	// Trong game thi nguoi choi chi duoc su dung luan phien 2 loai sung
	private int idGun1, idGun2; // id cua 2 loai sung trong game
	private int numberGun; // la so sung dung trong gamePlay
	private int numberWave = 0; // So dot moi chang
	private boolean isNextWave, isNextWave1;

	// float
	private float stateTime;
	private float timeExploseRocket; // thoi gian cho no rocket
	private float timeExploseGrenade; // thoi gian no luu dan
	private float timeGameOver, timeGameWin; // thoi gian thang thua
	private float timeReloadSkill; // thoi gian load lai skill
	private long timeFire = 0, timeFire1 = 0; // thoi gian ban
	private long timeRandom; // thoi gian random
	private long timeBeginFreze; // thoi gian dong bang
	private float x_touchDown, y_touchDown; // lay toan do khi touchDown
	private float x_TouchUp, y_TouchUp; // lay toan do khi touchUp
	private float angle; // goc

	// Rectangle
	// hinh cn bao lay skill khi no dung de test thui
	private Rectangle boundSkill;
	// Hinh chu nhat chon sung choi trong game(bam vao thi no doi sung)
	private Rectangle rectangleOption;
	private Rectangle recLocot;
	// Animation
	private Animation animationActor; // animation nhân vật chính
	private TextureRegion trCurrentFrame;

	// object
	private Map map;
	private InfoMap infoMap; // Luu so Zombie moi map
	private ArrayList<ExplosiveDevice> arrExplosive; // mang chua skill
	private Blockhouse blockhouse; // locot trong game
	private final ArrayList<Bullet> arrBullet; // mang dan

	private final ArrayList<BulletBoss> arrBulletBoss; // mang dan

	private ArrayList<GunShopObject> arrGun; // mang luu 2 loai sung trong game
	private ArrayList<Zombie> arrZombie; // mang luu so Zombie
	private Array<WaveZombie> arrWave; // chua cac chu dot 1 va dot cuoi
	private Array<PlusScore> arrPlusScore; // chua cac chu cong diem

	private FireObject fireObj; // skill no trong game
	// Dung Pool(doi tuong tai su dung duoc) cho doi tuong Bullet va Zombie
	private final Pool<Bullet> bulletPool;

	private final Pool<BulletBoss> bulletBossPool = new Pool<BulletBoss>() {
		@Override
		protected BulletBoss newObject() {
			return new BulletBoss();
		}

		@Override
		public void freeAll(Array<BulletBoss> objects) {
			super.freeAll(objects);
		}
	};

	private final Pool<Zombie> z1Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(1);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};

	private final Pool<Zombie> z2Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(2);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z3Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(3);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z4Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(4);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z5Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(5);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z6Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(6);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z7Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(7);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z8Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(8);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z9Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(9);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z10Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(10);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};
	private final Pool<Zombie> z11Pool = new Pool<Zombie>() {
		@Override
		protected Zombie newObject() {
			return new Zombie(11);
		}

		@Override
		public void freeAll(Array<Zombie> objects) {
			super.freeAll(objects);
		}
	};

	// 2 anh khi Die của 2 con Boss
	private Image imgBoss11Die, imgBoss10Die;
	// Mang cua mang chua so Zombie
	private ArrayList<ArrayList<Integer>> arrNumberZombie;
	// Cac chu chuyen dong trong game
	private MoveLetters moveThatBai, moveChien, moveThang;
	private ShapeRenderer renderer;

	// Mang luu con boss 11
	private ArrayList<Boss11> arrBoss11;

	// Mang luu con boss 10
	private ArrayList<Boss10> arrBoss10;

	private NipperLeft nipperLeft;
	private NipperRight nipperRight;

	public PlayScreen(final MyGame myGame) {
		super(myGame);
		stateGame = GAME_RUNNING;
		// Lay ve cac anh, nhac, font dung trong man hinh PlayScreen
		if (!Assets.GET_PLAY)
			Assets.getPlay();
		// Khởi tạo các button
		initButton();
		// Mo nhac nen trong game
		if (Assets.mOptionShop != null)
			Assets.mOptionShop.pause();
		// Cac bien kiem tra xem trang thai hien tai la musicOn hay musicOff,
		// soundOn hay soundOff
		isMusic = Setting.getBoolean(Setting.MUSIC);
		isSound = Setting.getBoolean(Setting.SOUND);
		isVibrate = Setting.getBoolean(Setting.VIBRATE);
		// Cho chay 1 trong 4 doan nhac nen game
		if (isMusic) {
			Assets.arrMusicGamePlay.get(MathUtils.random(0, 3)).play();
		} else {
			for (int i = 0; i < 4; i++) {
				if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
					Assets.arrMusicGamePlay.get(i).pause();
					break;
				}
			}
		}

		boundSkill = new Rectangle();
		rectangleOption = new Rectangle(624, 20, 55, 53);
		recLocot = new Rectangle(290, 0, 235, 105);

		animationActor = new Animation(0.1f, Assets.actor_Region);

		stateTime = 0;

		// Khoi tao cac doi tuong
		// map = new Map(Setting.getMap());
		map = new Map(Assets.map);
		fireObj = new FireObject();
		arrExplosive = new ArrayList<ExplosiveDevice>();
		arrExplosive.add(new ExplosiveDevice(1, Setting.getBoolean(1)));
		arrExplosive.add(new ExplosiveDevice(2, Setting.getBoolean(2)));
		arrExplosive.add(new ExplosiveDevice(3, Setting.getBoolean(3)));

		arrZombie = new ArrayList<Zombie>();

		// Lấy ra locot max
		for (int i = 0; i < 10; i++) {
			if (Setting.getLocot(i + 1))
				locot = i + 1;
		}
		blockhouse = new Blockhouse(locot);

		arrBullet = new ArrayList<Bullet>();
		arrBulletBoss = new ArrayList<BulletBoss>();

		arrWave = new Array<WaveZombie>();
		arrWave.add(new WaveZombie(0));
		arrWave.add(new WaveZombie(1));

		arrPlusScore = new Array<PlusScore>();

		// Doc du lieu tu infomap
		infoMap = new InfoMap(Assets.map);
		arrNumberZombie = new ArrayList<ArrayList<Integer>>();
		for (int k = 0; k < 6; k++) {
			arrNumberZombie.add(infoMap.getStage(k + 1));
			// Gdx.app.log(getClass().toString(), arrNumberZombie.size()
			// + " arrNumberZombie.size()");
		}
		// for (int i = 0; i < arrNumberZombie.size(); i++) {
		// for (int j = 0; j < arrNumberZombie.get(i).size(); j++) {
		// Gdx.app.log(
		// getClass().toString(),
		// "arrNumberZombie.get(i).get(j) "
		// + arrNumberZombie.get(i).get(j) + " + " + i
		// + " + " + j);
		// }
		// }
		// Khai bao cac thong so de dich chuyen cac chu Thất Bại, Chiến, Thắng
		moveThatBai = new MoveLetters(311, 0, 179, 60, Assets.trImage.get(5));
		moveChien = new MoveLetters(-109, 220, 109, 53, Assets.trImage.get(3));
		moveThang = new MoveLetters(800, 220, 125, 53, Assets.trImage.get(4));

		numberGun = Setting.arrGunsCanUse.size;
		idGun1 = Setting.arrGunsCanUse.get(numberGun - 1); // sung chinh
		// Neu nguoi choi chon tu 2 loai sung tro len thi se lay 2 sung nguoi
		// choi
		// chon cuoi cung lam sung chinh va sung phu
		if (numberGun >= 2)
			idGun2 = Setting.arrGunsCanUse.get(numberGun - 2); // sung phu

		bulletPool = new Pool<Bullet>() {
			@Override
			protected Bullet newObject() {
				return new Bullet(idGun1 - 1);
			}

			@Override
			public void freeAll(Array<Bullet> objects) {
				super.freeAll(objects);
			}
		};

		arrGun = new ArrayList<GunShopObject>();
		arrGun.add(new GunShopObject(idGun1));
		arrGun.add(new GunShopObject(idGun2));

		renderer = new ShapeRenderer();
		// Khoi tao so con Boss11 theo từng map
		Assets.numberBoss();
		arrBoss11 = new ArrayList<Boss11>();
		if (Assets.numBoss11 > 0) {
			for (int i = 0; i < Assets.numBoss11; i++) {
				arrBoss11.add(new Boss11());
			}
		}
		// Khoi tao so con Boss10 theo từng map
		arrBoss10 = new ArrayList<Boss10>();
		if (Assets.numBoss10 > 0) {
			for (int i = 0; i < Assets.numBoss10; i++) {
				arrBoss10.add(new Boss10());
			}
		}

		imgBoss11Die = fadeOutImage(Assets.trBoss11Die);
		imgBoss10Die = fadeOutImage(Assets.trBoss10Die);
		isMeetBoss = true;
	}

	// Cap nhat them tung con Zombie va random Zombie
	private void updateAddZombie(float time) {
		if (speed_Zombie > 0) {
			for (int j = 0; j < arrNumberZombie.get(Assets.stage - 1).size();) {
				int num;
				do {
					// Khi
					if (getTotalZombie() > getNumberZombie(Assets.stage) / 2
							|| (Assets.map == 10) || (Assets.map == 9))
						random = MathUtils.random(0, 10);
					else
						random = MathUtils.random(0, 8);
					num = getNum(random + 1);
				} while (arrNumberZombie.get(Assets.stage - 1).get(random) <= num);
				break;
			}
			// Sau 0.8s se them 1 con Zombie
			if (System.currentTimeMillis() - timeRandom > 800) {
				switch (random) {
				case 0:
					Zombie zb1 = z1Pool.obtain();
					arrZombie.add(zb1);
					num1++;
					break;
				case 1:
					Zombie zb2 = z2Pool.obtain();
					arrZombie.add(zb2);
					num2++;
					break;
				case 2:
					Zombie zb3 = z3Pool.obtain();
					arrZombie.add(zb3);
					num3++;
					break;
				case 3:
					Zombie zb4 = z4Pool.obtain();
					arrZombie.add(zb4);
					num4++;
					break;
				case 4:
					Zombie zb5 = z5Pool.obtain();
					arrZombie.add(zb5);
					num5++;
					break;
				case 5:
					Zombie zb6 = z6Pool.obtain();
					arrZombie.add(zb6);
					num6++;
					break;
				case 6:
					Zombie zb7 = z7Pool.obtain();
					arrZombie.add(zb7);
					num7++;
					break;
				case 7:
					Zombie zb8 = z8Pool.obtain();
					arrZombie.add(zb8);
					num8++;
					break;
				case 8:
					Zombie zb9 = z9Pool.obtain();
					arrZombie.add(zb9);
					num9++;
					break;
				case 9:
					Zombie zb10 = z10Pool.obtain();
					arrZombie.add(zb10);
					num10++;
					break;
				case 10:
					Zombie zb11 = z11Pool.obtain();
					arrZombie.add(zb11);
					num11++;
					break;
				}
				timeRandom = System.currentTimeMillis();
			}
		}
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		// batch.draw(Assets.ttMap.get(Assets.map - 1), 0, 0, 800, 480);//
		// Backround

		batch.draw(map.getTexture(), 0, 0, 800, 480);// Backround

		batch.draw(Assets.trImage.get(6), 0, -10, 800, 95); // khung ngang
		// Ve chon sung chinh va sung phu
		drawOptionGun();
		// Vẽ skill
		drawSkill();
		// Ve dialog Pause game
		drawDialogPause();

		batch.end();
		stage.act(delta);
		// Cap nhat trang thai cua game
		updateStateGame(delta);
		switch (stateGame) {
		case GAME_RUNNING:
			timeGameWin = 0;
			timeGameOver = 0;
			stateTime += 0.5 * Gdx.graphics.getDeltaTime();
			update(delta);
			batch.begin();
			// Ve dan
			drawBullet();
			// Ve Zombie va boss
			drawZombie();
			drawBoss();
			// Ve khi bam vao Skill
			drawPressSkill();
			// Ve chu diem cong va tru
			drawScoreMove();
			// Khi skill 1 va 3 san sang su dung ma touchDrag thi se ve hinh
			// theo toa do dich chuyen tren man hinh. Khi nguoi choi bo tay ra
			// se nổ
			drawSkillTouchDrag(x_touchDown, y_touchDown, typeSkill);
			// Ve trang thai bi Zombie hoac Boss tan cong
			drawZombieAttack();
			batch.end();
			// Ve duong bao quanh doi tuong dung de test va cham
			// renderDebug();
			// Ve hieu ung rung man hinh
			drawEffectVibrateScreen();

			break;
		case GAME_WIN:
			// Khi game o trang thai nay thi phai xoa mang arrBullet neu ko
			// khi ve trang thai Running nhung vien dan chua ban het se ban ra
			if (!arrBullet.isEmpty())
				for (int i = 0; i < arrBullet.size(); i++) {
					arrBullet.remove(i);
				}
			stateTime = 0;
			batch.begin();
			drawDialogWin();
			batch.end();
			break;
		case GAME_LOSE:
			stateTime = 0;
			batch.begin();
			drawDialogLose();
			batch.end();
			break;
		case GAME_WAVE:
			stateTime = 0;
			// Khi game o trang thai nay thi phai xoa mang arrBullet neu ko
			// khi ve trang thai Running nhung vien dan chua ban het se ban ra
			if (!arrBullet.isEmpty())
				for (int i = 0; i < arrBullet.size(); i++) {
					arrBullet.remove(i);
				}
			batch.begin();
			// Ve text dot 1 hoac dot cuoi
			drawWave();
			batch.end();
			// Cap nhat cac dot ra quai trong game
			updateWave(delta);
			break;
		}

		batch.begin();
		// Ve cac item trong GamePlay
		batch.draw(Assets.blockhouse_Region.get(locot - 1), 275, 0, 235, 109);// locot
		drawItemGamePlay();
		// Ve nhan vat nguoi ban va sung nen ve sau khi drawBullet
		drawActorGun();
		batch.end();
		stage.draw();

	}

	// Ve cac item trong game Play
	private void drawItemGamePlay() {

		Assets.font.draw(batch, "Điểm", 531, 473);
//		Assets.font.draw(batch, "fps:" + Gdx.graphics.getFramesPerSecond(),
//				360, 230);
		// blood
		batch.draw(Assets.trImage.get(7), 0, 405, 201, 81);
		batch.draw(Assets.trImage.get(8), 70, 431, 124 * blockhouse.getBlood()
				/ blockhouse.getMaxBlood(), 28);
		if (blockhouse.getBlood() < 0)
			Assets.font.draw(batch, "0", 104, 463);
		else
			Assets.font.draw(batch,
					String.valueOf((int) blockhouse.getBlood()), 104, 463);

		// task bao so con Zombie xuat hien
		batch.draw(Assets.trImage.get(9), 300, 450, 217, 20);
		batch.draw(Assets.trImage.get(10), 304, 454, 210, 14);

		// Ve kim bao so con Zombie da xuat hien tren man hinh
		batch.draw(Assets.trImage.get(11), 287 + 209 * getTotalZombie()
				/ getNumberZombie(Assets.stage), 433, 33, 42);
		// 2 hinh nen ghi so diem va so xu
		batch.draw(Assets.trImage.get(14), 200, 440, 97, 30);
		batch.draw(Assets.trImage.get(13), 598, 440, 97, 30);
		Assets.font.draw(batch, String.valueOf(Assets.coin), 220, 473);// diem
		Assets.font.draw(batch, String.valueOf(Assets.score), 603, 473);// xu
	}

	// Ve nhan vat nguoi ban va sung trong game
	private void drawActorGun() {
		trCurrentFrame = animationActor.getKeyFrame(stateTime, true);

		// Chi cho quay toi da 79 do
		if (angle > 79)
			angle = 79;
		else if (angle < -79)
			angle = -79;
		// batch.draw(Assets.gun_Region.get(idGun1 - 1), 390, 60, 10, -44,
		// Assets.gun_Region.get(0).getRegionWidth(), Assets.gun_Region
		// .get(0).getRegionHeight(), 1, 1, -angle);

		batch.draw(Assets.gun_Region.get(idGun1 - 1), 380, 50, 10, -44,
				Assets.gun_Region.get(0).getRegionWidth(), Assets.gun_Region
						.get(0).getRegionHeight(), 1, 1, -angle);
		// Ve nhan vat quay theo goc voi toa do diem quay la ((70, 30) chính la
		// tam cua anh lay dua theo toa do trong photoshop theo kich thuoc anh
		// 150*150)
		// batch.draw(trCurrentFrame, 325, -10, 70, 30, 150, 150, 1, 1, -angle);

		batch.draw(trCurrentFrame, 315, -20, 70, 30, 150, 150, 1, 1, -angle);

	}

	// Ve skill trong game
	private void drawSkill() {
		for (ExplosiveDevice ex : arrExplosive) {
			if (!ex.isBuy()) {
				// Neu ma skill bi khoa thi se ve anh lock
				batch.draw(
						Assets.trImage.get(12),
						ex.getBound().x - 3,
						ex.getId() == 3 ? ex.getBound().y + 2 : ex.getBound().y,
						69, 69);
			} else {
				// Skill khong bi khoa thi ve skill va thanh thoi gian su dung
				// lai skill
				timeReloadSkill += Gdx.graphics.getDeltaTime();
				if (timeReloadSkill > ex.getTimeReload()) {
					ex.setReload(true);
					timeReloadSkill = 0;
				}
				batch.draw(ex.getTexture(), ex.getX(), ex.getY(), 110, 110);
			}
		}
	}

	// Ve khi bam vao Skill
	private void drawPressSkill() {
		switch (typeSkill) {
		case 1:
			if (isRocket) {
				fireObj.create(x_TouchUp, y_TouchUp);
				// batch.draw(fireObj.getTexture(), fireObj.getX(),
				// fireObj.getY(),
				// 0,
				// 0, fireObj.getTexture().getRegionWidth(), fireObj
				// .getTexture().getRegionHeight(), 1, 1, -30);
				batch.draw(fireObj.getTexture(), x_TouchUp
						- (fireObj.getTexture().getRegionWidth() / 2),
						y_TouchUp
								- (fireObj.getTexture().getRegionHeight() / 2),
						fireObj.getTexture().getRegionWidth(), fireObj
								.getTexture().getRegionHeight());

				if (timeExploseRocket > 1) {
					typeSkill = 0;
					isRocket = false;
				}
			}
			break;
		case 2:
			if (speed_Zombie == 0) {
				arrExplosive.get(1).setReload(false);
				batch.draw(Assets.ttBgFreeze, 0, 0, Assets.SCREEN_WIDTH,
						Assets.SCREEN_HEIGHT);
			}
			break;
		case 3:
			if (isGrenade) {
				fireObj.create(x_TouchUp, y_TouchUp);

				batch.draw(
						fireObj.getTexture1(),
						x_TouchUp
								- (fireObj.getTexture1().getRegionWidth() / 2)
								- 10,
						y_TouchUp
								- (fireObj.getTexture1().getRegionHeight() / 2 - 10),
						fireObj.getTexture1().getRegionWidth() + 20, fireObj
								.getTexture1().getRegionHeight() + 20);

				if (timeExploseGrenade > 1.5f) {
					typeSkill = 0;
					isGrenade = false;
				}
			}
			break;
		default:
			break;
		}

	}

	// Ve sung chinh va sung phu trong game
	private void drawOptionGun() {
		batch.draw(Assets.trGunOption.get(idGun1 - 1), 502, 16, 60, 60);
		if (numberGun >= 2)
			batch.draw(Assets.trGunOption.get(idGun2 - 1), 620, 16, 60, 60);
	}

	boolean rung;
	float time_rung;

	// Tao ra danh sach dan
	private void creatBullet(float time) {
		if (isFire && stateGame == GAME_RUNNING) {
			// Thoi gian moi lan them dan la 150milis = 0.15s
			float timeFireBullet = isGun1 ? arrGun.get(0).getTimeFire()
					: arrGun.get(1).getTimeFire();
			if (System.currentTimeMillis() - timeFire > 1000 * timeFireBullet) {
				if (isSound)
					Assets.arrSoundGun.get(idGun1 - 1).play(1); // am thanh ban
				// Su dung Pool de tai su dung cac doi tuong thay vi tao moi
				// Bullet b = new Bullet(0);
				Bullet b;
				b = bulletPool.obtain();
				b.createFunction(x_touchDown, y_touchDown);
				arrBullet.add(b);
				timeFire = System.currentTimeMillis();
				rung = true;
			}
		}
	}

	// Tao ra danh sach dan
	private void creatBulletBoss(float time) {
		if (isAppearBoss) {
			if (!arrBoss11.isEmpty()) {
				for (int i = 0; i < arrBoss11.size(); i++) {
					if (arrBoss11.get(i).getState_Boss() == 1) {
						// Thoi gian moi lan them dan la 150milis = 0.15s
						if (System.currentTimeMillis() - timeFire1 > 1500) {
							// if (isSound)
							// Assets.arrSoundGun.get(idGun1 - 1).play(1); // am
							// thanh
							// ban
							BulletBoss b;
							b = bulletBossPool.obtain();
							b.setPosXY(arrBoss11.get(i).getBounds().x + 81,
									arrBoss11.get(i).getBounds().y);
							arrBulletBoss.add(b);
							timeFire1 = System.currentTimeMillis();
						}
					}
				}
			}
		}
	}

	// ve dan
	private void drawBullet() {
		if (modePlay == MODE_BAN) {
			if (!arrBullet.isEmpty()) {
				int len = arrBullet.size();
				for (int i = 0; i < len; i++) {
					if (arrBullet.get(i).isFly()) {
						batch.draw(
								arrBullet.get(i).getTexture(),
								arrBullet.get(i).bounds.x - 7,
								arrBullet.get(i).bounds.y,
								x1,
								y1,
								arrBullet.get(i).getTexture().getRegionWidth(),
								arrBullet.get(i).getTexture().getRegionHeight(),
								1, 1, -arrBullet.get(i).getAngle());
					} else {
						arrBullet.remove(i);
						len--;
					}
				}
			}

			if (!arrBulletBoss.isEmpty()) {

				int len = arrBulletBoss.size();
				for (int i = 0; i < len; i++) {
					if (arrBulletBoss.get(i).isFly()) {
						batch.draw(arrBulletBoss.get(i).getTexture(),
								arrBulletBoss.get(i).bounds.x,
								arrBulletBoss.get(i).bounds.y,
								arrBulletBoss.get(i).bounds.width,
								arrBulletBoss.get(i).bounds.height);

					} else {
						arrBulletBoss.remove(i);
						len--;
					}
				}
			}
		}

	}

	// Ve Zombie, Boss
	private void drawZombie() {
		TextureRegion key;
		int len = arrZombie.size() - 1;
		for (int i = len; i >= 0; i--) {

			if (!arrZombie.get(i).isRemove()) {
				key = arrZombie.get(i).getTexture();
				if (arrZombie.get(i).getDirect() == 3) {
					batch.draw(key, arrZombie.get(i).bounds.x
							+ arrZombie.get(i).getW_ZOMBIE(),
							arrZombie.get(i).bounds.y, -arrZombie.get(i)
									.getW_ZOMBIE(), arrZombie.get(i)
									.getH_ZOMBIE());
				} else {
					batch.draw(key, arrZombie.get(i).bounds.x,
							arrZombie.get(i).bounds.y, arrZombie.get(i)
									.getW_ZOMBIE(), arrZombie.get(i)
									.getH_ZOMBIE());
				}
			}
		}

	}

	private void drawBoss() {
		if (isAppearBoss) {
			if (!arrBoss11.isEmpty()) {
				TextureRegion key1;
				for (int i = arrBoss11.size() - 1; i >= 0; i--) {
					if (arrBoss11.get(i).getState_Boss() < 2) {
						key1 = arrBoss11.get(i).getTexture();
						batch.draw(key1, arrBoss11.get(i).getBounds().x,
								arrBoss11.get(i).getBounds().y, arrBoss11
										.get(i).getBounds().width, arrBoss11
										.get(i).getBounds().height);
					} else if (arrBoss11.get(i).getState_Boss() == 2) {
						imgBoss11Die.setPosition(
								arrBoss11.get(i).getBounds().x, arrBoss11
										.get(i).getBounds().y);
						imgBoss11Die.setSize(
								arrBoss11.get(i).getBounds().width, arrBoss11
										.get(i).getBounds().height);
						stage.addActor(imgBoss11Die);
					} else {
						stage.getRoot().removeActor(imgBoss11Die);
					}
				}
			}

			// Ve con boss 10 va 2 cai càng cua

			if (!arrBoss10.isEmpty()) {
				TextureRegion key2;
				for (int i = arrBoss10.size() - 1; i >= 0; i--) {
					if (arrBoss10.get(i).getState_Boss() == 0) {
						key2 = arrBoss10.get(i).getTexture();
						batch.draw(key2, arrBoss10.get(i).getBounds().x,
								arrBoss10.get(i).getBounds().y, arrBoss10
										.get(i).getBounds().width, arrBoss10
										.get(i).getBounds().height);
						// Càng cua bên trái
						batch.draw(arrBoss10.get(i).getTextureLeft(), arrBoss10
								.get(i).getBounds().x - 25, arrBoss10.get(i)
								.getBounds().y - 36, 82, 80);
						// Càng cua bên phải
						batch.draw(arrBoss10.get(i).getTextureLeft(), arrBoss10
								.get(i).getBounds().x + 165, arrBoss10.get(i)
								.getBounds().y - 36, -82, 80);
					} else if (arrBoss10.get(i).getState_Boss() == 1) {
						key2 = arrBoss10.get(i).getTexture();
						batch.draw(key2, arrBoss10.get(i).getBounds().x,
								arrBoss10.get(i).getBounds().y, arrBoss10
										.get(i).getBounds().width, arrBoss10
										.get(i).getBounds().height);
						if (!check) {
							// Càng cua bên trái di chuyển
							nipperLeft = new NipperLeft();
							nipperLeft.setPosition(
									arrBoss10.get(i).getBounds().x, arrBoss10
											.get(i).getBounds().y - 36);
							nipperLeft.setWidth(82);
							nipperLeft.setHeight(80);
							stage.addActor(nipperLeft);
							check = true;
							nipperLeft.addAction(Actions.forever(Actions
									.sequence(Actions.moveTo(310, 95,
											(2 - 0.1f * Assets.map)), Actions
											.moveTo(arrBoss10.get(i)
													.getBounds().x, arrBoss10
													.get(i).getBounds().y - 36,
													(2 - 0.1f * Assets.map)))));
							// Càng cua bên phải di chuyển
							nipperRight = new NipperRight();
							nipperRight.setPosition(arrBoss10.get(i)
									.getBounds().x, arrBoss10.get(i)
									.getBounds().y - 36);
							nipperRight.setWidth(82);
							nipperRight.setHeight(80);
							stage.addActor(nipperRight);
							check = true;
							nipperRight
									.addAction(Actions.sequence(
											Actions.delay(2 - 0.1f * Assets.map),
											Actions.forever(Actions.sequence(
													Actions.moveTo(
															310,
															95,
															(2 - 0.1f * Assets.map)),
													Actions.moveTo(
															arrBoss10
																	.get(i)
																	.getBounds().x,
															arrBoss10
																	.get(i)
																	.getBounds().y - 36,
															(2 - 0.1f * Assets.map))))));
						}
					} else if (arrBoss10.get(i).getState_Boss() == 2) {
						if (nipperLeft != null)
							nipperLeft.setStateNipper(1);
						if (nipperRight != null)
							nipperRight.setStateNipper(1);
						imgBoss10Die.setPosition(
								arrBoss10.get(i).getBounds().x, arrBoss10
										.get(i).getBounds().y);
						imgBoss10Die.setSize(
								arrBoss10.get(i).getBounds().width, arrBoss10
										.get(i).getBounds().height);
						stage.addActor(imgBoss10Die);
					} else {
						stage.getRoot().removeActor(imgBoss10Die);
						stage.getRoot().removeActor(nipperLeft);
						stage.getRoot().removeActor(nipperRight);
					}
				}
			}
		}
	}

	// Ve cac chu dot 1 va dot cuoi
	private void drawWave() {
		for (int i = 0; i < arrWave.size; i++) {
			if (arrWave.get(i).isStart()) {
				batch.draw(arrWave.get(i).getMyTexture(),
						arrWave.get(i).bounds.x, arrWave.get(i).bounds.y,
						arrWave.get(i).bounds.getWidth(),
						arrWave.get(i).bounds.getHeight());
			}
		}
	}

	// Ve chu cong diem di chuyen va mo dan
	private void drawScoreMove() {
		// Ve cong diem
		if (arrPlusScore.size > 0) {
			for (int i = 0; i < arrPlusScore.size; i++) {
				ArrayList<TextureRegion> texture = arrPlusScore.get(i)
						.getTexture();
				for (int j = 0; j < texture.size(); j++) {
					// Cho mo dan chu cong diem
					batch.setColor(arrPlusScore.get(i).getAnpha(), 1, 1,
							arrPlusScore.get(i).getAnpha());

					if (j == 0)
						// Ve chu cong o vi tri cao hon so 10pixel va co chieu
						// cao =
						// 2/3 chu so
						batch.draw(texture.get(0), arrPlusScore.get(i).getX(),
								arrPlusScore.get(i).getY() + 10, arrPlusScore
										.get(i).getWidthOneNumber(),
								arrPlusScore.get(i).getHeight() * 2 / 3);
					else
						// ve so
						batch.draw(texture.get(j), arrPlusScore.get(i).getX()
								+ j * arrPlusScore.get(i).getWidthOneNumber(),
								arrPlusScore.get(i).getY(), arrPlusScore.get(i)
										.getWidthOneNumber(),
								arrPlusScore.get(i).getHeight());
					batch.setColor(Color.WHITE);
				}
			}
		}

	}

	// Ve di chuyen cac skill khi touchDrag
	// Alt + shift + j
	/**
	 * @param screenX
	 *            tọa độ x touchDown
	 * @param screenY
	 *            tọa độ y touchDown
	 * @param type
	 *            loại skill trong game
	 */
	private void drawSkillTouchDrag(float screenX, float screenY, int type) {
		if (typeSkill > 0 && modePlay == MODE_SKILL) {
			float x = screenX
					- Assets.trSkillImage.get(type - 1).getRegionWidth() / 2;
			float y = screenY
					- Assets.trSkillImage.get(type - 1).getRegionHeight() / 2;

			if (type == 1)
				batch.draw(Assets.trSkillImage.get(0), x, y,
						Assets.trSkillImage.get(0).getRegionWidth(),
						Assets.trSkillImage.get(0).getRegionHeight());
			else if (type == 3)
				batch.draw(Assets.trSkillImage.get(2), x, y,
						Assets.trSkillImage.get(2).getRegionWidth(),
						Assets.trSkillImage.get(2).getRegionHeight());
		}
	}

	public void renderDebug() {
		renderer.setProjectionMatrix(camera.combined);
		renderer.begin(ShapeType.Rectangle);

		// for (Zombie zb : arrZombie) {
		// renderer.rect(zb.getBound().x, zb.getBound().y,
		// zb.getBound().width, zb.getBound().height);
		// }
		//
		// if (nipperLeft != null)
		// renderer.rect(nipperLeft.getBounds().x, nipperLeft.getBounds().y,
		// nipperLeft.getBounds().width, nipperLeft.getBounds().height);
		//
		// if (nipperRight != null)
		// renderer.rect(nipperRight.getBounds().x, nipperRight.getBounds().y,
		// nipperRight.getBounds().width,
		// nipperRight.getBounds().height);
		// for (BulletBoss bb : arrBulletBoss) {
		// renderer.rect(bb.bounds.x, bb.bounds.y, bb.bounds.width,
		// bb.bounds.height);
		// }
		//
		// for (ExplosiveDevice ex : arrExplosive) {
		// renderer.rect(ex.getBound().x, ex.getBound().y,
		// ex.getBound().width, ex.getBound().height);
		// }
		//
		// renderer.rect(recLocot.x, recLocot.y, recLocot.width,
		// recLocot.height);
		// renderer.rect(rectangleOption.x, rectangleOption.y,
		// rectangleOption.width, rectangleOption.height);
		// if (isRocket || isGrenade)
		// renderer.rect(boundSkill.x, boundSkill.y, boundSkill.width,
		// boundSkill.height);

		renderer.end();
	}

	// Ve dialog pause game
	private void drawDialogPause() {
		if (isPause) {
			stateTime = 0;
			dialogPause.setBounds(212, 152, 365, 176);
			stage.addActor(dialogPause);
		}
	}

	private boolean isSoundWin = true, isSoundLose = true;

	// Ve dialog
	private void drawDialogWin() {
		timeGameWin += Gdx.graphics.getDeltaTime();
		if (timeGameWin > 1)
			batch.draw(Assets.ttBgWin, 115, 121, Assets.ttBgWin.getWidth(),
					Assets.ttBgWin.getHeight());
		if (isSoundWin) {
			for (int i = 0; i < 4; i++) {
				if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
					Assets.arrMusicGamePlay.get(i).pause();
					break;
				}
			}
			if (Assets.mMeetBoss.isPlaying())
				Assets.mMeetBoss.pause();
			if (isSound)
				Assets.arrSoundGamePlay.get(12).play(1);
			isSoundWin = false;
		}
		moveChien.moveRight(timeGameWin);
		moveThang.moveLeft(timeGameWin);

		batch.draw(moveChien.getTexure(), moveChien.position.x,
				moveChien.position.y, moveChien.bounds.width,
				moveChien.bounds.height);

		batch.draw(moveThang.getTexure(), moveThang.position.x,
				moveThang.position.y, moveThang.bounds.width,
				moveThang.bounds.height);

		Assets.effectStar1.draw(batch, 0.02f);
		if (isDialogWin) {
			// Khi game o trang thai nay thi phai xoa mang arrBullet neu ko
			// khi ve trang thai Running nhung vien dan chua ban het se ban ra
			arrBullet.clear();
			dialogWin.setBounds(212, 130, 365, 218);
			stage.addActor(dialogWin);
			isDialogWin = false;
		}
	}

	private void drawDialogLose() {
		timeGameOver += Gdx.graphics.getDeltaTime();
		if (isSoundLose) {
			if (isSound) {
				Assets.arrSoundGamePlay.get(11).play();
				Gdx.app.log("", "Co vao nhac thua ko");
			}
			if (nipperLeft != null)
				stage.getRoot().removeActor(nipperLeft);
			if (nipperRight != null)
				stage.getRoot().removeActor(nipperRight);
			for (int i = 0; i < 4; i++) {
				if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
					Assets.arrMusicGamePlay.get(i).pause();
					break;
				}
			}
			if (Assets.mMeetBoss.isPlaying())
				Assets.mMeetBoss.pause();
			Gdx.app.error("State", "Nhạc thua");

			if (timeGameOver > 1f)
				isSoundLose = false;
		}
		batch.draw(Assets.trImage.get(16), 0, 0, 212, Assets.SCREEN_HEIGHT);
		batch.draw(Assets.trImage.get(17), 537, 0, 263, Assets.SCREEN_HEIGHT);
		moveThatBai.moveUp(timeGameOver);
		batch.draw(moveThatBai.getTexure(), moveThatBai.position.x,
				moveThatBai.position.y, moveThatBai.bounds.width,
				moveThatBai.bounds.height);
		if (isDialogLose) {
			// Khi game o trang thai nay thi phai xoa mang arrBullet neu ko
			// khi ve trang thai Running nhung vien dan chua ban het se ban ra
			arrBullet.clear();
			// Ve dialog Lose
			dialogLose.setBounds(212, 130, 365, 218);
			stage.addActor(dialogLose);
			isDialogLose = false;
		}
	}

	// Ve trang thai bi Zombie hoac Boss tan cong
	private void drawZombieAttack() {
		// Zombie
		for (int i = 0; i < arrZombie.size(); i++) {
			if (arrZombie.get(i).getState_Zombie() == 1 && !blockhouse.isLose()) {
				batch.draw(Assets.trImage.get(16), 0, 0, 212,
						Assets.SCREEN_HEIGHT);
				batch.draw(Assets.trImage.get(17), 537, 0, 263,
						Assets.SCREEN_HEIGHT);
				break;
			}
		}

		// Boss 11
		if (isAppearBoss) {
			if (!arrBoss11.isEmpty())
				for (int i = 0; i < arrBoss11.size(); i++) {
					if (arrBoss11.get(i).getState_Boss() == 1
							&& !blockhouse.isLose()) {
						batch.draw(Assets.trImage.get(16), 0, 0, 212,
								Assets.SCREEN_HEIGHT);
						batch.draw(Assets.trImage.get(17), 537, 0, 263,
								Assets.SCREEN_HEIGHT);
						break;
					}
				}

			// Boss 10
			if (!arrBoss10.isEmpty())
				for (int i = 0; i < arrBoss10.size(); i++) {
					if (arrBoss10.get(i).getState_Boss() == 1
							&& !blockhouse.isLose()) {
						batch.draw(Assets.trImage.get(16), 0, 0, 212,
								Assets.SCREEN_HEIGHT);
						batch.draw(Assets.trImage.get(17), 537, 0, 263,
								Assets.SCREEN_HEIGHT);
						break;
					}
				}
		}
	}

	// ---------------------------------------------------UPDATE-------------------------------------------------------

	// Cap nhat cac ham trang thai trong game Play
	private void update(float time) {
		if (!isPause && !Assets.IS_GAMEOVER) {
			// Goi cac ham update trang thai game
			// Tao dan ban ra cua nguoi choi
			creatBullet(time);
			// Tao dan cua con Boss
			creatBulletBoss(time);
			// Cap nhat khi va cham giua dan va zombie
			updateCollision(time);
			// Xu ly va cham voi boss
			updateCollisionBoss(time);
			// Cap nhat khi va cham vat can
			updateCollisionBarrier(time);
			// Cap nhat lai vi tri Zombie
			updateZombie(time);
			// Cap nhat lai vi tri cua dan
			updateBullet(time);
			// Cập nhật thoi gian animation cho skill
			updateTimeAnimationSkill(time);
			// Cập nhật khi nguoi choi dung skill
			updateUserSkill(time);
			// Cap nhat chu dich chuyen
			updateMinusPlusScore(time);
			// Cap nhat trang thai Zombie tan cong
			updateZombieAttack(time);
			// Cap nhat trang thai Boss tan cong
			updateBossAttack(time);
			// Cap nhat, thiet lap cac chu Dot 1 va Dot Cuoi xuat hien o cac man
			updateAddWave(time);
			// Cap nhat tam quay cua dan
			updateOrigin(time);
		}
	}

	// Cap nhat lai vi tri Zombie
	private void updateZombie(float time) {
		int len = arrZombie.size();
		for (int i = 0; i < len; i++) {
			arrZombie.get(i).update(time, speed_Zombie);
		}

		if (isAppearBoss) {
			if (!arrBoss11.isEmpty())
				for (int i = 0; i < arrBoss11.size(); i++) {
					arrBoss11.get(i).update(time, speed_Zombie);
				}
			if (!arrBoss10.isEmpty())
				for (int i = 0; i < arrBoss10.size(); i++) {
					arrBoss10.get(i).update(time, speed_Zombie);
				}
		}
	}

	// Cap nhat lai vi tri Bullet
	private void updateBullet(float time) {
		int len = arrBullet.size();
		for (int i = 0; i < len; i++) {
			arrBullet.get(i).move(time);
		}

		int len1 = arrBulletBoss.size();
		for (int i = 0; i < len1; i++) {
			arrBulletBoss.get(i).move(time);
		}
	}

	// Cap nhat khi va cham giua dan va zombie
	private void updateCollision(float time) {
		int len = arrZombie.size();
		int len1 = arrBullet.size();
		if (idGun1 < 5) {
			for (int i = 0; i < len; i++) {
				for (int j = 0; j < len1; j++) {
					if (OverlapTester.pointInRectangle(
							arrZombie.get(i).getBound(),
							arrBullet.get(j).position.x,
							arrBullet.get(j).position.y + 20
									* MathUtils.sinDeg(Math.abs(angle)))) {
						if (isSound)
							Assets.arrSoundGamePlay.get(10).play(1);
						// Lay ra so mau cua sung moi lan ban
						int dame = 0;

						if (isGun1) {
							dame = arrGun.get(0).getDamage();
							// Gdx.app.log("", dame + " dame 1");
						} else {
							dame = arrGun.get(1).getDamage();
							// Gdx.app.log("", dame + " dame 2");
						}
						arrZombie.get(i).shot(dame);
						arrBullet.remove(j);
						len1--;
					}
				}
			}
		} else {
			// if (OverlapTester.overlapRectangles(arrZombie.get(i).getBound(),
			// arrBullet.get(j).getBound())) {
			// if (isSound)
			// Assets.arrSoundGamePlay.get(10).play(1);
			// // Lay ra so mau cua sung moi lan ban
			// int dame = 0;
			//
			// if (isGun1) {
			// dame = arrGun.get(0).getDamage();
			// // Gdx.app.log("", dame + " dame 1");
			// } else {
			// dame = arrGun.get(1).getDamage();
			// // Gdx.app.log("", dame + " dame 2");
			// }
			// arrZombie.get(i).shot(dame);
			// arrBullet.remove(j);
			// len1--;
			// }
			for (int i = 0; i < arrBullet.size(); i++) {
				for (Zombie zb : arrZombie) {
					if (zb.position.x - (idGun1 == 5 ? 50 : 75) <= arrBullet
							.get(i).position.x
							&& zb.position.x + (idGun1 == 5 ? 50 : 75) >= arrBullet
									.get(i).position.x
							&& zb.position.y - (idGun1 == 5 ? 50 : 75) <= arrBullet
									.get(i).position.y
							&& zb.position.y + (idGun1 == 5 ? 50 : 75) >= arrBullet
									.get(i).position.y) {
						int dame;
						if (isGun1) {
							dame = arrGun.get(0).getDamage();
							// Gdx.app.log("", dame + " dame 1");
						} else {
							dame = arrGun.get(1).getDamage();
							// Gdx.app.log("", dame + " dame 2");
						}
						zb.shot(dame);
					}
				}
			}
		}
		if (isRocket || isGrenade) {
			for (Zombie zb : arrZombie) {
				int damage = 0;
				if (typeSkill == 1)
					damage = 50;
				else if (typeSkill == 3)
					damage = 80;
				if (zb.position.x - damage <= x_TouchUp
						&& zb.position.x + damage >= x_TouchUp
						&& zb.position.y - damage <= y_TouchUp
						&& zb.position.y + damage >= y_TouchUp) {
					if (typeSkill > 0) {
						int dame = arrExplosive.get(typeSkill - 1).getDamage();
						zb.shot(dame);
					}
				}
			}
		}

		Iterator<Zombie> iter = arrZombie.iterator();
		while (iter.hasNext()) {
			Zombie z = iter.next();
			if (z.isDie()) {
				if (!isRocket && !isGrenade)
					arrPlusScore.add(new PlusScore(z.getScore(), z.bounds.x,
							z.bounds.y));
				z.setDie(false); // de cho chi chay vao if nay 1 lan
			}
			if (z.isRemove()) {
				Assets.score += z.getScore(); // cong diem
				iter.remove();
				numberZombieDie++;
				// Khi so Zombie chết = số con Zombie mỗi chặng thì win
				if (numberZombieDie == getNumberZombie(Assets.stage)) {
					if (isHasBoss) {
						isAppearBoss = true;
						if (isMeetBoss) {
							for (int i = 0; i < 4; i++) {
								if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
									Assets.arrMusicGamePlay.get(i).pause();
									break;
								}
							}
							if (isMusic)
								Assets.mMeetBoss.play();
							isMeetBoss = false;
						}
					} else
						Assets.IS_GAMEWIN = true;
				}
			}
		}
	}

	private boolean isMeetBoss;
	public boolean isHasBoss;// Có Boss hay ko
	public boolean isAppearBoss; // Xuat hien Boss

	// Cap nhat khi va cham voi Boss
	private void updateCollisionBoss(float time) {
		// Va cham giua dan cua nhan vat chinh va boss 11
		if (isAppearBoss) {
			if (!arrBoss11.isEmpty()) {
				int len = arrBoss11.size();
				int len1 = arrBullet.size();
				for (int i = 0; i < len; i++) {
					for (int j = 0; j < len1; j++) {
						if (OverlapTester.overlapRectangles(arrBoss11.get(i)
								.getBounds(), arrBullet.get(j).bounds)) {
							if (isSound)
								Assets.arrSoundGamePlay.get(10).play(1);
							// Lay ra so mau cua sung moi lan ban
							int dame;
							if (isGun1)
								dame = arrGun.get(0).getDamage();
							else
								dame = arrGun.get(1).getDamage();
							arrBoss11.get(i).shot(dame);
							arrBullet.remove(j);
							len1--;
						}
					}
				}
				if (isRocket || isGrenade) {
					for (Boss11 zb : arrBoss11) {
						if (OverlapTester.pointInRectangle(zb.getBounds(),
								x_TouchUp, y_TouchUp)) {
							if (typeSkill > 0) {
								int dame = arrExplosive.get(typeSkill - 1)
										.getDamage();
								zb.shot(dame);
							}

						}

					}
				}

				Iterator<Boss11> iter = arrBoss11.iterator();
				while (iter.hasNext()) {
					Boss11 z = iter.next();
					if (z.getState_Boss() == 3) {
						if (!isRocket && !isGrenade)
							arrPlusScore.add(new PlusScore(z.getScore(), z
									.getBounds().x, z.getBounds().y));
						Assets.score += z.getScore(); // cong diem
						iter.remove();
						Assets.numBoss11--;
						if (Assets.numBoss11 == 0 && Assets.numBoss10 == 0)
							Assets.IS_GAMEWIN = true;
					}
				}
			}
			// Va cham giua dan cua nhan vat chinh va boss 10
			if (!arrBoss10.isEmpty()) {
				int length = arrBoss10.size();
				int length1 = arrBullet.size();
				for (int i = 0; i < length; i++) {
					for (int j = 0; j < length1; j++) {
						if (OverlapTester.overlapRectangles(arrBoss10.get(i)
								.getBounds(), arrBullet.get(j).bounds)
								&& (arrBoss10.get(i).getState_Boss() < 2)) {
							if (isSound)
								Assets.arrSoundGamePlay.get(10).play(1);
							// Lay ra so mau cua sung moi lan ban
							int dame;
							if (isGun1)
								dame = arrGun.get(0).getDamage();
							else
								dame = arrGun.get(1).getDamage();
							arrBoss10.get(i).shot(dame);
							arrBullet.remove(j);
							length1--;
						}
					}
				}
				if (isRocket || isGrenade) {
					for (Boss10 zb : arrBoss10) {
						if (OverlapTester.pointInRectangle(zb.getBounds(),
								x_TouchUp, y_TouchUp)) {
							if (typeSkill > 0) {
								int dame = arrExplosive.get(typeSkill - 1)
										.getDamage();
								zb.shot(dame);
							}
						}
					}
				}

				Iterator<Boss10> iter1 = arrBoss10.iterator();
				while (iter1.hasNext()) {
					Boss10 z = iter1.next();
					if (z.getState_Boss() == 3) {
						if (!isRocket && !isGrenade)
							arrPlusScore.add(new PlusScore(z.getScore(), z
									.getBounds().x, z.getBounds().y));
						Assets.score += z.getScore(); // cong diem
						iter1.remove();
						Assets.numBoss10--;
						if (Assets.numBoss11 == 0 && Assets.numBoss10 == 0)
							Assets.IS_GAMEWIN = true;
					}
				}
			}
		}
	}

	// Cap nhat khi va cham voi vat can
	private void updateCollisionBarrier(float time) {
		if (!arrBullet.isEmpty()) {
			for (int i = 0; i < map.getArrPolygon().size(); i++) {
				for (int j = 0; j < arrBullet.size(); j++) {
					if (map.getArrPolygon()
							.get(i)
							.contains(
									arrBullet.get(j).position.x
											+ arrBullet.get(j).bounds.width / 2,
									arrBullet.get(j).position.y
											+ arrBullet.get(j).bounds.height
											/ 2)) {
						if (isSound)
							Assets.arrSoundGamePlay.get(0).play(1);
						arrBullet.remove(j);
					}
				}
			}
		}
	}

	private void updateStateGame(float time) {
		if (Assets.stage == 1)
			isHasBoss = true;
		else
			isHasBoss = false;

		if (Assets.IS_GAMEOVER)
			stateGame = GAME_LOSE;
		else if (Assets.IS_GAMEWIN)
			stateGame = GAME_WIN;
	}

	// Cap nhat thoi gian cho animation cua skill
	private void updateTimeAnimationSkill(float time) {
		if (isRocket || isGrenade) {
			boundSkill.setX(fireObj.getX());
			boundSkill.setY(fireObj.getY());
			boundSkill.setWidth(200);
			boundSkill.setHeight(200);
			fireObj.update(time);
		} else
			fireObj.reset(time);

		for (ExplosiveDevice ex : arrExplosive) {
			ex.update(time);
		}
	}

	// Cap nhat khi cham vao skill
	private void updateUserSkill(float time) {
		switch (typeSkill) {
		case 1:
			// Khi bam vao skill rocket
			if (isRocket)
				timeExploseRocket += time;
			else {
				fireObj.reset(time);
				timeExploseRocket = 0;
			}
			break;

		case 2:
			// Skill dong bang se co tac dung trong 3s
			if (speed_Zombie == 0) {
				if (System.currentTimeMillis() - timeBeginFreze > 3000) {
					typeSkill = 0;
					speed_Zombie = 1;
				}
			}
			break;
		case 3:
			// Khi bam vao Skill Grenade(luu dan)
			if (isGrenade)
				timeExploseGrenade += time;
			else {
				fireObj.reset(time);
				timeExploseGrenade = 0;
			}
			break;
		default:
			break;
		}

	}

	// Cap nhat cac dot ra quai trong game
	private void updateWave(float time) {
		for (int i = 0; i < arrWave.size; i++) {
			if (arrWave.get(i).isStart()) {
				arrWave.get(i).move(time);
			}
			if (Assets.stage == 2 || Assets.stage == 3) {
				if (arrWave.get(1).isFinish1()) {
					isNextWave = true;
					arrWave.get(1).setStart(false);
					stateGame = GAME_RUNNING;
				}
			} else if (Assets.stage > 3) {
				if (arrWave.get(0).isFinish1() && numberWave == 1) {
					isNextWave = true;
					arrWave.get(0).setStart(false);
					stateGame = GAME_RUNNING;
				}

				if (arrWave.get(1).isFinish1() && numberWave == 2) {
					isNextWave1 = true;
					arrWave.get(0).setStart(false);
					arrWave.get(1).setStart(false);
					stateGame = GAME_RUNNING;
				}
			}
		}

	}

	// Cap nhat chu cong diem va tru diem
	private void updateMinusPlusScore(float time) {
		if (arrPlusScore.size > 0) {
			for (PlusScore ps : arrPlusScore) {
				if (ps.isEnd()) {
					arrPlusScore.removeValue(ps, true);
				} else {
					ps.move(time);
				}
			}

		}
	}

	// Cap nhat trang thai Zombie Attack
	private void updateZombieAttack(float time) {
		for (int i = 0; i < arrZombie.size(); i++) {
			if (arrZombie.get(i).getState_Zombie() == 1 && !blockhouse.isLose()
					&& !arrZombie.get(i).isDie()) {
				if (isSound)
					Assets.arrSoundGamePlay.get(1).play(1);
				blockhouse.decreaseBlood(arrZombie.get(i).getDamageSpeed(),
						time / (20 - 0.5f * Assets.map));
			}
			if (blockhouse.isLose()) {
				Assets.IS_GAMEOVER = true;
				break;
			}
		}
	}

	private void updateBossAttack(float time) {
		if (isAppearBoss) {
			if (!arrBoss10.isEmpty()) {
				// Va cham khi Boss10 tan cong locot
				if (nipperLeft != null) {
					timeCol += time;
					if ((OverlapTester.overlapRectangles(recLocot,
							nipperLeft.getBounds()))
							|| ((OverlapTester.overlapRectangles(recLocot,
									nipperRight.getBounds())))) {
						if (timeCol > 2.9f)
							isCol = true;

						if (isCol) {
							if (isSound)
								Assets.arrSoundGamePlay.get(1).play(1);
							isCol = false;
							blockhouse.decreaseBlood(arrBoss10.get(0)
									.getDamageSpeed(), time);
							timeCol = 0;
						}

						if (blockhouse.isLose()) {
							Assets.IS_GAMEOVER = true;
						}
					}
				}
			}

			if (!arrBoss11.isEmpty()) {
				// Va cham giua dan cua Boss11 va locot
				for (int i = 0; i < arrBulletBoss.size(); i++) {
					if (OverlapTester.overlapRectangles(
							arrBulletBoss.get(i).bounds, recLocot)) {
						for (int j = 0; j < arrBoss11.size(); j++) {

							if (arrBoss11.get(j).getState_Boss() == 1
									&& !blockhouse.isLose()) {
								if (isSound)
									Assets.arrSoundGamePlay.get(1).play(1);
								blockhouse.decreaseBlood(arrBoss11.get(j)
										.getDamageSpeed(), time);
							}
							if (blockhouse.isLose()) {
								Assets.IS_GAMEOVER = true;
								Gdx.app.log(getClass().toString(),
										"Thua roi em oi " + Assets.IS_GAMEOVER);
								break;
							}
							arrBulletBoss.remove(i);
						}
					}
				}
			}
		}
	}

	private float timeCol;
	private boolean isCol;

	// Cap nhat them cac chu dot 1 va dot cuoi trong game
	private void updateAddWave(float time) {
		if (Assets.stage == 1) {
			// Chang nay ko co chu nao ca, zombie ra 1 lan
			if (getTotalZombie() < getNumberZombie(Assets.stage)) {
				updateAddZombie(time);
			}
		} else if (Assets.stage == 2 || Assets.stage == 3) {
			// Chang 2 va 3 zombie ra thanh 2 dot
			// Neu nguoi choi ban chet duoc 10 con zombie thi xuat hien chu
			// Dot Cuoi
			if (getTotalZombie() < 100) {
				updateAddZombie(time);
			} else if (getTotalZombie() == 100) {
				if (numberZombieDie == 100) {
					arrWave.get(1).setStart(true);
					stateGame = GAME_WAVE;
				}
			}
			if (isNextWave == true) {
				if (getTotalZombie() < getNumberZombie(Assets.stage)) {
					updateAddZombie(time);
				}
			}
		} else {
			// Cac chang nay quai ra thanh 3 dot
			if (getTotalZombie() < 100) {
				updateAddZombie(time);
			}
			// khi nguoi choi ban chet dot nay se xuat hien chu Dot 1
			else if (getTotalZombie() == 100 && !isNextWave) {
				if (numberZombieDie == 100) {
					arrWave.get(0).setStart(true);
					numberWave = 1;
					stateGame = GAME_WAVE;
				}
			} else if (getTotalZombie() < 200 && isNextWave) {
				updateAddZombie(time);
			}
			// khi nguoi choi ban chet dot nay se xuat hien chu Dot Cuoi
			else if (getTotalZombie() == 200 && !isNextWave1) {
				if (numberZombieDie == 200) {
					arrWave.get(1).setStart(true);
					numberWave = 2;
					stateGame = GAME_WAVE;
				}
			} else if (getTotalZombie() < getNumberZombie(Assets.stage)
					&& isNextWave1) {
				updateAddZombie(time);
			}
		}
	}

	// Cap nhat tam quay cua dan
	private void updateOrigin(float time) {
		switch (idGun1) {
		case 1:
			x1 = 11;
			y1 = 36;
			break;
		case 2:
			x1 = 13;
			y1 = 40;
			break;
		case 3:
			x1 = 15;
			y1 = 42;
			break;
		case 4:
			x1 = 13;
			y1 = 44;
			break;
		case 5:
			x1 = 14;
			y1 = 49;
			break;
		case 6:
			x1 = 22;
			y1 = 76;
			break;
		}
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		super.resize(width, height);
	}

	@Override
	public void show() {
		super.show();
	}

	@Override
	public void pause() {
		if (stateGame != GAME_LOSE && stateGame != GAME_WIN)
			isPause = true;
		for (int i = 0; i < 4; i++) {
			if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
				Assets.arrMusicGamePlay.get(i).pause();
				break;
			}
		}
	}

	@Override
	public void resume() {
		super.resume();
		if (isMusic)
			Assets.arrMusicGamePlay.get(MathUtils.random(0, 3)).play();
		else
			for (int i = 0; i < 4; i++) {
				if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
					Assets.arrMusicGamePlay.get(i).pause();
					break;
				}
			}
	}

	@Override
	public void dispose() {
		super.dispose();
//		Assets.disposeObjects();
		imgBoss11Die.clearActions();
		arrBullet.clear();
		arrExplosive.clear();
		arrZombie.clear();
		arrGun.clear();
		arrPlusScore.clear();
		dialogPause.clear();
		dialogLose.clear();
		dialogWin.clear();
		if (arrBoss10 != null)
			arrBoss10.clear();
		if (arrBoss11 != null)
			arrBoss11.clear();
		if (arrBulletBoss != null)
			arrBulletBoss.clear();
		if (nipperLeft != null) {
			nipperLeft = null;
		}
		if (nipperRight != null) {
			nipperRight = null;
		}

		map = null;

		blockhouse = null;
		boundSkill = null;

		if (blockhouse != null)
			blockhouse = null;
		Assets.arrMusicGamePlay.clear();
		Assets.arrSoundGamePlay.clear();
		Assets.arrSoundGun.clear();
		if (Assets.sClick != null)
			Assets.sClick.dispose();
		if (Assets.mOptionShop != null)
			Assets.mOptionShop.dispose();
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.BACK && stateGame != GAME_LOSE
				&& stateGame != GAME_WIN) {
			isDialogLose = false;
			isDialogWin = false;
			isPause = true;
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		return super.keyUp(keycode);
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		x_touchDown = screenX * Assets.SCREEN_WIDTH / Gdx.graphics.getWidth();
		y_touchDown = (Gdx.graphics.getHeight() - screenY)
				* Assets.SCREEN_HEIGHT / Gdx.graphics.getHeight();

		switch (stateGame) {
		case GAME_RUNNING:
			if (y_touchDown > 80 && modePlay != MODE_SKILL) {
				if (!isPause) {
					isFire = true;

					angle = (float) Math.toDegrees(Math
							.atan((x_touchDown - Dimension.CENTER_SCREEN_X)
									/ y_touchDown));
				}
			}
			if (modePlay != MODE_SKILL && speed_Zombie == 1) {
				for (ExplosiveDevice ex : arrExplosive) {
					if (ex.isUse()) {
						if (OverlapTester.pointInRectangle(ex.getBound(),
								x_touchDown, y_touchDown)) {
							// Nếu chạm vào skill
							typeSkill = ex.getId();
							modePlay = MODE_SKILL;
						}
						if (typeSkill == 2) {
							speed_Zombie = 0;
							timeBeginFreze = System.currentTimeMillis();
							if (isSound)
								Assets.arrSoundGamePlay.get(6).play(1);
						}
					}
				}
			}
			if (OverlapTester.pointInRectangle(rectangleOption, x_touchDown,
					y_touchDown)) {
				if (isSound)
					Assets.arrSoundGamePlay.get(2).play(1);
				// Doi sung chinh va sung phu cho nhau
				if (numberGun >= 2) {
					isGun1 = !isGun1;
					idGun1 += idGun2;
					idGun2 = idGun1 - idGun2;
					idGun1 -= idGun2;
				}
			}
			break;
		case GAME_WIN:
			isFire = false;
			if (!arrBullet.isEmpty())
				for (int i = 0; i < arrBullet.size(); i++) {
					arrBullet.remove(i);
				}
			isDialogWin = true;
			break;
		case GAME_LOSE:
			isFire = false;
			isDialogLose = true;
			break;
		case GAME_WAVE:
			isFire = false;
			if (!arrBullet.isEmpty())
				for (int i = 0; i < arrBullet.size(); i++) {
					arrBullet.remove(i);
				}
			break;
		}

		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		x_TouchUp = screenX * Assets.SCREEN_WIDTH / Gdx.graphics.getWidth();
		y_TouchUp = (Gdx.graphics.getHeight() - screenY) * Assets.SCREEN_HEIGHT
				/ Gdx.graphics.getHeight();

		switch (stateGame) {
		case GAME_RUNNING:
			isFire = false;
			if (modePlay == MODE_SKILL) {
				if (typeSkill == 1) {
					arrExplosive.get(0).setUse(false);
					isRocket = true;
				} else if (typeSkill == 2) {
					if (isSound)
						Assets.arrSoundGamePlay.get(5).play(1); // nhac no
					arrExplosive.get(1).setUse(false);
					isGrenade = true;
				} else if (typeSkill == 3) {
					if (isSound)
						Assets.arrSoundGamePlay.get(4).play(1); // nhac no
					arrExplosive.get(2).setUse(false);
					isGrenade = true;
				}
				modePlay = MODE_BAN;
			}

			break;
		case GAME_WIN:
			isFire = false;
			if (!arrBullet.isEmpty())
				for (int i = 0; i < arrBullet.size(); i++) {
					arrBullet.remove(i);
				}
			break;
		case GAME_LOSE:
			isFire = false;
			break;
		case GAME_WAVE:
			isFire = false;
			if (!arrBullet.isEmpty())
				for (int i = 0; i < arrBullet.size(); i++) {
					arrBullet.remove(i);
				}
			break;
		}
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		x_touchDown = screenX * Assets.SCREEN_WIDTH / Gdx.graphics.getWidth();
		y_touchDown = (Gdx.graphics.getHeight() - screenY)
				* Assets.SCREEN_HEIGHT / Gdx.graphics.getHeight();
		switch (stateGame) {
		case GAME_RUNNING:
			if (y_touchDown > 80 && modePlay != MODE_SKILL) {
				if (!isPause) {
					isFire = true;
					angle = (float) Math.toDegrees(Math
							.atan((x_touchDown - Dimension.CENTER_SCREEN_X)
									/ y_touchDown));
				}
			}
			if (modePlay != MODE_SKILL && speed_Zombie == 1)
				for (ExplosiveDevice ex : arrExplosive) {
					// Neu skill trong trang thai co the su dung
					if (ex.isUse()) {
						if (OverlapTester.pointInRectangle(ex.getBound(),
								x_touchDown, y_touchDown)) {
							// Nếu chạm vào skill
							typeSkill = ex.getId();
							modePlay = MODE_SKILL;
						}
					}
				}

			break;

		case GAME_WIN:
			break;
		case GAME_LOSE:
			break;
		}
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return super.scrolled(amount);
	}

	private void initButton() {
		// Nut pause
		btnPause = new Button(Assets.skin, "btnPause");
		btnPause.setHeight(300);
		btnPause.setBounds(730, 409, 69, 68);
		btnPause.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.VIBRATE))
					Gdx.input.vibrate(200);
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				arrBullet.clear();
				isFire = false;
				isPause = true;
			}
		});
		stage.addActor(btnPause);

		dialogPause = new Dialog("", new WindowStyle(Assets.font, Color.WHITE,
				new TextureRegionDrawable(new TextureRegion(
						Assets.trImage.get(0)))));
		// Nut quit
		btnMenuPause = new Button(Assets.skin, "btnMenu");
		btnMenuPause.setBounds(35, 63, 100, 35);
		btnMenuPause.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);

				resetVariable();
				isPause = false;
				game.request.trackEndLevel(Assets.map, Assets.score);
				game.setScreen(new StageScreen(game));
			}
		});
		stage.addActor(btnMenuPause);

		// Nut resume
		btnTiepTucGame = new Button(Assets.skin, "btnTiepTucGame");
		btnTiepTucGame.setBounds(228, 63, 100, 35);
		btnTiepTucGame.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				isPause = false;
				dialogPause.remove();
			}
		});

		dialogPause.addActor(btnMenuPause);
		dialogPause.addActor(btnTiepTucGame);

		// --------------------------------------------------------DIALOG
		// WIN------------------------------------------------------
		// Dialog hien ra khi win
		dialogWin = new Dialog("", new WindowStyle(Assets.font, Color.WHITE,
				new TextureRegionDrawable(new TextureRegion(
						Assets.trImage.get(2)))));

		// Nut btnGuiDiem
		btnGuiDiemWin = new Button(Assets.skin, "btnGuiDiem");
		btnGuiDiemWin.setBounds(23, 67, 100, 35);
		btnGuiDiemWin.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				// if (Setting.getCoin() < 10000)
				// game.dialog.notEnoughtCoin();
				// else
				game.request.sendScore(Assets.score);
			}
		});

		// Nut btnRetry
		btnRetry1 = new Button(Assets.skin, "btnRetry");
		btnRetry1.setBounds(137, 67, 90, 35);
		btnRetry1.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				for (int i = 0; i < 4; i++) {
					if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
						Assets.arrMusicGamePlay.get(i).pause();
						break;
					}
				}
				resetVariable();
				game.setScreen(new ShopGun(game));
			}
		});
		// Nut btnNext
		btnTiepTucGame = new Button(Assets.skin, "btnTiepTucGame");
		btnTiepTucGame.setBounds(241, 67, 100, 35);
		btnTiepTucGame.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				dialogWin.remove();
				resetVariable();
				Assets.stage++;
				if (Assets.stage > Assets.MAX_STAGE) {
					game.request.trackEndLevel(Assets.map, Assets.score);
					Assets.map++;
					game.request.trackStartLevel(Assets.map);
					Assets.stage = 1;
					if (Assets.map > Assets.MAX_MAP) {
						Assets.map = Assets.MAX_MAP;
					}
					Setting.putBoolean("MAP" + Assets.map, true);
					game.setScreen(new ShopGun(game));
				}
				Setting.putBoolean("STAGE" + Assets.map + "" + Assets.stage,
						true);
				// Setting.setStage(Assets.stage);
			}
		});
		// Add 3 nut vao dialog
		dialogWin.addActor(btnGuiDiemWin);
		dialogWin.addActor(btnRetry1);
		dialogWin.addActor(btnTiepTucGame);

		// ----------------------------------------------------DIALOG
		// LOSE--------------------------------------------------------
		// Dialog hien ra khi Lose
		dialogLose = new Dialog("", new WindowStyle(Assets.font, Color.WHITE,
				new TextureRegionDrawable(new TextureRegion(
						Assets.trImage.get(1)))));

		// Nut btnGuiDiem
		btnMenuLose = new Button(Assets.skin, "btnMenu");
		btnMenuLose.setBounds(23, 67, 100, 35);
		btnMenuLose.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				for (int i = 0; i < 4; i++) {
					if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
						Assets.arrMusicGamePlay.get(i).pause();
						break;
					}
				}
				resetVariable();
				game.setScreen(new StageScreen(game));
			}
		});

		// Nut btnRetry
		btnRetry2 = new Button(Assets.skin, "btnRetry");
		btnRetry2.setBounds(137, 67, 90, 35);
		btnRetry2.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				for (int i = 0; i < 4; i++) {
					if (Assets.arrMusicGamePlay.get(i).isPlaying()) {
						Assets.arrMusicGamePlay.get(i).pause();
						break;
					}
				}
				resetVariable();
				game.setScreen(new ShopGun(game));
			}
		});

		// Nut btnGuiDiem
		btnGuiDiemLose = new Button(Assets.skin, "btnGuiDiem");
		btnGuiDiemLose.setBounds(241, 67, 100, 35);
		btnGuiDiemLose.addListener(new ClickListener() {
			public void clicked(InputEvent event, float x, float y) {
				if (Setting.getBoolean(Setting.SOUND))
					Assets.sClick.play(1);
				// if (Setting.getCoin() < 10000)
				// game.dialog.notEnoughtCoin();
				// else
				game.request.sendScore(Assets.score);
			}
		});
		// Add 2 nut vao dialog
		dialogLose.addActor(btnMenuLose);
		dialogLose.addActor(btnRetry2);
		dialogLose.addActor(btnGuiDiemLose);
	}

	// Hieu ung giat man hinh
	public void drawEffectVibrateScreen() {
		if (rung && isVibrate) {
			float timeFireBullet = isGun1 ? arrGun.get(0).getTimeFire()
					: arrGun.get(1).getTimeFire();
			// if (System.currentTimeMillis() - timeFire > 1000 *
			// timeFireBullet) {
			// Gdx.app.log("", " co rung ko");
			// camera.zoom = 0.98f;
			// timeFire = System.currentTimeMillis();
			// }
			time_rung += Gdx.graphics.getDeltaTime();
			if (time_rung < timeFireBullet) {
				camera.zoom = 0.98f;
			} else {
				camera.zoom = 1f;
				rung = false;
				time_rung = 0;
			}
		} else {
			camera.zoom = 1;
		}
	}

	private int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10,
			num11;

	// Tra ve tong so con Zombie da xuat hien tren man hinh
	private int getTotalZombie() {
		return num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9
				+ num10 + num11;
	}

	// reset lai cac bien khi sang chang moi
	private void resetVariable() {
		if (!arrBullet.isEmpty())
			// for (int i = 0; i < arrBullet.size(); i++) {
			// arrBullet.remove(i);
			// }
			arrBullet.removeAll(arrBullet);
		isSoundLose = true;
		isSoundWin = true;
		Assets.IS_GAMEOVER = false;
		Assets.IS_GAMEWIN = false;
		numberZombieDie = 0;
		isNextWave = false;
		isNextWave1 = false;
		num1 = num2 = num3 = num4 = num5 = num6 = num7 = num8 = num9 = num10 = num11 = 0;
		for (int i = 0; i < arrWave.size; i++) {
			arrWave.get(i).setFinish1(false);
			arrWave.get(i).resetPosition();
		}
		stateGame = GAME_RUNNING;
	}

	private int getNum(int id) {
		int i = 0;
		switch (id) {
		case 1:
			i = num1;
			break;

		case 2:
			i = num2;
			break;
		case 3:
			i = num3;
			break;
		case 4:
			i = num4;
			break;
		case 5:
			i = num5;
			break;
		case 6:
			i = num6;
			break;
		case 7:
			i = num7;
			break;
		case 8:
			i = num8;
			break;
		case 9:
			i = num9;
			break;
		case 10:
			i = num10;
			break;
		case 11:
			i = num11;
			break;
		}
		return i;
	}

	// Tra ve so con Zombie tren moi chang(stage)
	private int getNumberZombie(int stage) {
		switch (stage) {
		case 1:
			numberZombie = 100;
			break;

		case 2:
			numberZombie = 150;
			break;
		case 3:
			numberZombie = 200;
			break;
		case 4:
			numberZombie = 300;
			break;
		case 5:
			numberZombie = 300;
			break;
		case 6:
			numberZombie = 300;
			break;
		}
		return numberZombie;
	}

	/**
	 * @param texture
	 * @return anh de lam mo dan va bien mat sau 2s
	 */
	private Image fadeOutImage(TextureRegion texture) {
		Image image = new Image(texture);
		// Cho hien anh len sau do lam mo dan anh
		image.addAction(Actions.sequence(Actions.fadeIn(0), Actions.fadeOut(2)));
		return image;
	}

}
